import { GetRequest, PostRequest, PutRequest, DeleteRequest } from '@/utils/request'
import type { Response } from '@/utils/request'
import store from '@/store'
import { useAuthStore } from '@/store/auth'
import aesHelper from '@/utils/aesHelper'
import { doEncrypt } from '@/utils/smHelper'
import netconfig from '@/config/netconfig'

const authStore = useAuthStore(store)
type PhoneToken = {
  token: any
  code: number
  data: {
    code: number
    data: {
      token: any
    }
  }
}
// 获取openid
type GetOpenIdRes = { openid: string; unionid: string }
export function getOpenId(code: string): Promise<Response<GetOpenIdRes>> {
  return GetRequest<GetOpenIdRes>(`/api/v1/wx/account/getOpenidAndSessionKey`, { code }, { noAuth: true })
}

/**
 * 通过手机号获取token
 */
export function getPhoneToken(params: { loginid: any; unionid: any; openid: any; type: any; env: any }) {
  return new Promise((resolve, reject) => {
    const { loginid, unionid, openid, type, env } = params
    requirePlugin(
      'login-plugin',
      async (loginPlugin) => {
        let res = await loginPlugin.getPhoneToken({ loginid, unionid, openid, type, env })
        resolve(res)
      },
      ({ mod, errMsg }) => {
        reject()
      }
    )
  })
}

/**
 * 解密HCMP小程序手机号
 */
export function decryptPhone(sign: string, phoneData: string): Promise<Response<string>> {
  return PostRequest(
    '/api/account/verifyAccount',
    {
      sign: sign,
      encryptDate: phoneData,
    },
    { noAuth: true }
  )
}

/**
 * 获取Agreement
 */
export type AgreementItem<Type> = {
  url: string
  version: string
  agreementType: Type
  bool: null
}
export type AgreementInfo = {
  TOU: AgreementItem<'TOU'>
  DPI: AgreementItem<'DPI'>
  DCBTI: AgreementItem<'DCBTI'>
}
export function getAgreementInfo(): Promise<Response<AgreementInfo>> {
  return GetRequest('/api/v1/wx/account/agreement/' + netconfig.MINI_TYPE, void 0, { noAuth: true })
}

/**
 * 注销账号
 */
export function delentAccount() {
  return PutRequest('/api/v1/wx/account/v1/deregister', {
    encryptedData: doEncrypt(
      JSON.stringify({
        loginid: '+86' + aesHelper.decrypt(uni.getStorageSync('ciamId'), authStore.key, authStore.iv),
        unionid: aesHelper.decrypt(uni.getStorageSync('unionid'), authStore.key, authStore.iv),
        openid: authStore.accessOpenId,
        type: netconfig.MINI_TYPE,
      })
    ),
  })
}

/**
 * 获取用户附加信息
 */
export type UserAccountAddition = {
  valleyStarttime: string
  valleyEndtime: string
}
export function getUserAddition() {
  return GetRequest<UserAccountAddition>('/api/v1/wx/account/addition')
}

/**
 * 设置用户附加信息
 */
export function setUserAddition(data: {}) {
  return PostRequest('/api/v1/wx/account/addition', data)
}

/**
 * 获取Wifi列表
 */
export type WifiList = {}
export function getWifiList() {
  return GetRequest<WifiList[]>('/api/wx/province/wxuserPairWiFis')
}

/**
 * 删除用户所有的wifi信息
 */
export function deleteWxuserPairWiFis() {
  return DeleteRequest('/api/wx/province/deleteWxuserPairWiFis')
}

/**
 * native账号的用户名
 */
export type WxDetail = {
  country: string
  email: string
  firstname: string
  lastname: string
  phone: string
}
export function getWxDetail() {
  return GetRequest<WxDetail>('/api/v1/wx/account/wxDetail')
}

/**
 * 修改用户名
 */
export function changeNickname(name: string) {
  return PostRequest<string>('/api/v1/wx/account/v2/rename/' + netconfig.MINI_TYPE, {
    firstname: name.substr(1, name.length - 1),
    lastname: name.substr(0, 1),
  })
}

export type EncryptPhoneModel = {
  sign: string
  encryptDate: string
}

/**
 * 加密小程序用户手机号
 */
export function encryptUserPhone(): Promise<Response<EncryptPhoneModel>> {
  return GetRequest('/api/account/getAccountEncryptAndSign', void 0, { ignoreErrorTips: true })
}
